<template>
  <div>
<!--    <h2>{{ note.title || "标题" }}</h2>-->
    <input v-model="note.title" placeholder="标题" />
    <quill-editor v-model:content="note.content" contentType="html"></quill-editor>
    <button @click="saveNote">保存</button>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
// import { ipcRenderer } from 'electron';
import { QuillEditor } from '@vueup/vue-quill';
import '@vueup/vue-quill/dist/vue-quill.snow.css';
import {ipc} from "@/utils/ipcRenderer";
import {ipcApiRoute} from "@/api";

const props = defineProps({
  note: {
    type: Object,
    required: true
  }
});

const note = ref({ ...props.note });

const saveNote = async () => {
  console.log("saveNote----------------",JSON.stringify(note.value))
  ipc.invoke(ipcApiRoute.windows.saveNotes, {title: note.value.title, content: note.value.content});
  // if (note.value.id) {
  //   // 更新便签
  //   await fetch('/api/notes/' + note.value.id, {
  //     method: 'PUT',
  //     headers: {
  //       'Content-Type': 'application/json'
  //     },
  //     body: JSON.stringify(note.value)
  //   })
  //       .then(response => response.json())
  //       .then(data => {
  //         console.log('Note updated:', data);
  //         ipcRenderer.send('refreshNotesList');
  //       })
  //       .catch(error => console.error('Error updating note:', error));
  // } else {
  //   // 添加新便签
  //   await fetch('/api/notes', {
  //     method: 'POST',
  //     headers: {
  //       'Content-Type': 'application/json'
  //     },
  //     body: JSON.stringify(note.value)
  //   })
  //       .then(response => response.json())
  //       .then(data => {
  //         console.log('Note added:', data);
  //         ipcRenderer.send('refreshNotesList');
  //       })
  //       .catch(error => console.error('Error adding note:', error));
  // }
};

function init() {
  ipc.on('edit-index', async (event, result) => {
    console.log('edit-index-------:', result);

    // note.value = await ipc.invoke("getNotesById");
  })
}

onMounted(() => {
  console.log('onMounted-------:');
  // 初始化 Quill 编辑器
  init();
  note.value = ipc.invoke(ipcApiRoute.windows.getNotesById, 16);
});
</script>
